﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using GemWorkshop.Formats.Nitro;
using GemWorkshop.Pokemon;

namespace GemWorkshop.Trainer
{
    public partial class TrainerEditor : Form
    {
        Narc trPoke, trData;
        PokemonLoader pl;
        TrainerLoader tl;
        int currentPoke, currentTrainer;

        bool mc = false;

        public TrainerEditor()
        {
            InitializeComponent();

            this.trPoke = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "TrPokemonData"));
            this.trData = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "TrData"));
            pl = new PokemonLoader(null, null, null);
            tl = new TrainerLoader(this.trPoke, this.trData);

            listTrainers.Items.Clear();
            for (ushort i = 0; i < this.trData.FileCount; i++)
            {
                listTrainers.Items.Add(string.Format("Trainer {0}", i));
            }

            for (int i = 0; i < 500; i++) // 500 pokemon
            {
                cSpecies.Items.Add(pl.GetName(i));
            }

            chkUniqueItems.Enabled = false;
            chkUniqueMoves.Enabled = false;
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            try
            {
                TrainerSaver ts = new TrainerSaver(tl);
                ts.Save(currentTrainer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
            }
        }

        private void bCross_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listTrainers_SelectedIndexChanged(object sender, EventArgs e)
        {
            tl.LoadTrainer(listTrainers.SelectedIndex);
            currentTrainer = listTrainers.SelectedIndex;

            chkUniqueMoves.Checked = tl.trainer.UniqueMoves;
            chkUniqueItems.Checked = tl.trainer.UniqueItems;

            cClass.SelectedIndex = tl.trainer.Class;
            chkDBattle.Checked = tl.trainer.DoubleBattle;

            cItem1.SelectedIndex = tl.trainer.Item1;
            cItem2.SelectedIndex = tl.trainer.Item2;
            cItem3.SelectedIndex = tl.trainer.Item3;
            cItem4.SelectedIndex = tl.trainer.Item4;

            listPokemon.Items.Clear();
            for (int i = 0; i < tl.pokemon.Length; i++)
            {
                listPokemon.Items.Add(string.Format("{0} - {1}", tl.pokemon[i].Level, pl.GetName(tl.pokemon[i].Species)));
            }
        }

        private void chkUniqueMoves_CheckedChanged(object sender, EventArgs e)
        {
            gAttacks.Enabled = chkUniqueMoves.Checked;
        }

        private void chkUniqueItems_CheckedChanged(object sender, EventArgs e)
        {
            cItem.Enabled = chkUniqueItems.Checked;
        }

        private void listPokemon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mc) return;
            
            currentPoke = listPokemon.SelectedIndex;

            cSpecies.SelectedIndex = tl.pokemon[currentPoke].Species;
            nLevel.Value = tl.pokemon[currentPoke].Level;

            if (tl.trainer.UniqueItems)
            {
                cItem.SelectedIndex = tl.pokemon[currentPoke].Item;
            }
            else
            {
                cItem.SelectedIndex = 0;
            }

            if (tl.trainer.UniqueMoves)
            {
                cAttack1.SelectedIndex = tl.pokemon[currentPoke].Attack1;
                cAttack2.SelectedIndex = tl.pokemon[currentPoke].Attack2;
                cAttack3.SelectedIndex = tl.pokemon[currentPoke].Attack3;
                cAttack4.SelectedIndex = tl.pokemon[currentPoke].Attack4;
            }
            else
            {
                cAttack1.SelectedIndex = 0;
                cAttack2.SelectedIndex = 0;
                cAttack3.SelectedIndex = 0;
                cAttack4.SelectedIndex = 0;
            }
        }

        private void bModify_Click(object sender, EventArgs e)
        {
            tl.pokemon[currentPoke].Species = (ushort)cSpecies.SelectedIndex;
            tl.pokemon[currentPoke].Level = (ushort)nLevel.Value;

            if (tl.trainer.UniqueItems)
            {
                tl.pokemon[currentPoke].Item = (ushort)cItem.SelectedIndex;
            }

            if (tl.trainer.UniqueMoves)
            {
                tl.pokemon[currentPoke].Attack1 = (ushort)cAttack1.SelectedIndex;
                tl.pokemon[currentPoke].Attack2 = (ushort)cAttack2.SelectedIndex;
                tl.pokemon[currentPoke].Attack3 = (ushort)cAttack3.SelectedIndex;
                tl.pokemon[currentPoke].Attack4 = (ushort)cAttack4.SelectedIndex;
            }

            mc = true;
            listPokemon.Items[listPokemon.SelectedIndex] = string.Format("{0} - {1}", tl.pokemon[currentPoke].Level, pl.GetName(tl.pokemon[currentPoke].Species));
            mc = false;
        }
    }
}
